/**
 * 基础菜单 商品管理
 */
<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>仓库人员管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索筛选 -->
    <el-form :inline="true" :model="condition" class="user-search">
      <el-form-item label="搜索：">
        <el-input
          size="small"
          v-model="condition.name"
          placeholder="输入人员姓名"
        ></el-input>
      </el-form-item>

      <el-form-item label="">
        <el-select
          size="small"
          v-model="condition.sex"
          placeholder="请选择人员性别"
        >
          <el-option label="性别" value=""></el-option>
         <el-option label="男" value="01"></el-option>
          <el-option label="女" value="00"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="">
        <el-select
          size="small"
          v-model="condition.status"
          placeholder="请选择材料状态"
        >
          <el-option label="状态" value=""></el-option>
          <el-option label="正常" value="01"></el-option>
          <el-option label="已冻结" value="00"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button
          size="small"
          type="primary"
          icon="el-icon-search"
          @click="search"
          >搜索</el-button
        >
        <el-button
          size="small"
          type="primary"
          icon="el-icon-plus"
          @click="handleEdit('add')"
          >添加</el-button
        >
      </el-form-item>
    </el-form>
    <!--列表-->

    <div>
      <el-button
        size="mini"
        type="danger"
        @click="dels"
        style="margin-bottom: 5px"
        >批量删除</el-button
      >
    </div>

    <el-table
      size="small"
      :data="listData"
      @selection-change="selectChange"
      highlight-current-row
      v-loading="loading"
      border
      element-loading-text="拼命加载中"
      style="width: 100%"
    >
      <el-table-column align="center" type="selection" width="60">
      </el-table-column>
      
      <!-- 人员姓名 -->
      <el-table-column sortable prop="name" label="人员姓名" min-width="150">
      </el-table-column>

      <!-- 性别 -->
      <el-table-column sortable prop="sex" label="性别" min-width="150" :formatter="sexFormat">
      </el-table-column>

      <!-- 联系方式 -->
      <el-table-column sortable prop="phone" label="联系方式" min-width="150" >
      </el-table-column>

      <!-- 创建时间 -->
      <el-table-column
        sortable
        prop="createTime"
        label="创建时间"
        min-width="150"
      >
      </el-table-column>

      <!-- 修改时间 -->
      <el-table-column
        sortable
        prop="updateTime"
        label="修改时间"
        min-width="150"
      >
      </el-table-column>

      <!-- 状态 -->
       <el-table-column prop="status" label="状态" min-width="80">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            active-value="01"
            inactive-value="00"
            active-color="#13ce66"
            inactive-color="#ff4949"
            @change="setStatus(scope.row.id, scope.row.status)"
          >
          </el-switch>
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作" min-width="300">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="success"
            @click="handleEdit('view', scope.row)"
            >详情</el-button
          >

          <el-button size="mini" @click="handleEdit('edit', scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="del( scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <Pagination
      v-bind:child-msg="pageparm"
      @callFather="callFather"
    ></Pagination>
    <!-- 编辑界面 -->
    <el-dialog
      :title="title"
      :visible.sync="editFormVisible"
      width="30%"
      @close="closeDialog"
    >
      <el-form
        label-width="100px"
        :model="editForm"
        :rules="rules"
        ref="editForm"
      >
  
        <el-form-item label="人员姓名" prop="name" >
          <el-input
            size="small"
            v-model="editForm.name"
            auto-complete="off"
            placeholder="请输入仓库人员姓名"
            :disabled="isView"
          ></el-input>
        </el-form-item>
        
        <el-form-item label="性别" prop="sex">
          <el-radio-group v-model="editForm.sex" size="small" :disabled="isView">
            <el-radio label="01" border>男</el-radio>
            <el-radio label="00" border>女</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="联系方式" prop="phone">
          <el-input
            size="small"
            v-model="editForm.phone"
            auto-complete="off"
            placeholder="请输入联系方式"
            :disabled="isView"
          ></el-input>
        </el-form-item>

        <el-form-item label="家庭住址" prop="address">
          <el-input
              size="small"
              v-model="editForm.address"
              auto-complete="off"
              placeholder="请输入家庭住址"
              :disabled="isView"
            ></el-input>
        </el-form-item>

        <el-form-item label="详情描述" prop="remarks">
          <el-input type="textarea" v-model="editForm.remarks" placeholder="请输入仓库人员的详情描述" :rows="3" :disabled="isView"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeDialog">取消</el-button>
        <el-button
          size="small"
          type="primary"
          :loading="loading"
          class="title"
          @click="submitForm('editForm')"
          >保存</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Pagination from "../../components/Pagination";
import dataOrigin from "../../assets/data.json";
export default {
  inject: ["reload"],
  data() {
    
    return {
      loading: false, //是显示加载
      editFormVisible: false, //控制编辑页面显示与隐藏
      isView:false, //是否为详情页面
      isAdd:false, //是否为添加页面
      title: "添加",
      types: dataOrigin.materialTypes,
      editForm: {
        id: "",
        name: "",
        sex: "",
        phone: "",
        address:"",
        remarks:"",
        status: "",
      },
      // rules表单验证
      rules: {
        name: [
          { required: true, message: "请输入仓库人员", trigger: "blur" },
        ],
        sex: [
          { required: true, message: "请选择人员性别", trigger: "blur" },
        ],
        phone: [
          { required: true, message: "请输入联系方式", trigger: "blur" },
          {
            pattern: /^1(3\d|47|5((?!4)\d)|7(0|1|[6-8])|8\d)\d{8,8}$/,
            required: true,
            message: "请输入正确的手机号",
            trigger: "blur",
          }
        ],
        address: [
          { required: true, message: "请输入家庭住址", trigger: "blur" },
          
        ],
      },
      condition: {
        currentPage: 1,
        pageSize: 10,
        name: "",
        sex: "",
        status: "",
      },
      listData: [], //用户数据
      // 选择数据
      selectdata: [],
      // 分页参数
      pageparm: {
        currentPage: 1,
        pageSize: 10,
        total: 0,
      },
    };
  },
  // 注册组件
  components: {
    Pagination,
  },

  /**
   * 创建完毕
   */
  created() {
    this.init();
  },

  /**
   * 里面的方法只有被调用才会执行
   */
  methods: {
    //初始化表格
    async init() {
      this.loading = true;
      var d = await this.$axios.post("/admin/page",this.condition);
      if(d.code==200){
        this.listData = d.data.records;
        this.pageparm.currentPage = d.data.current;
        this.pageparm.pageSize = d.data.size;
        this.pageparm.total = d.data.total;
      }else{
        this.$message.error(d.message);
      }
      this.loading = false;
    },

     //修改用户状态
    async setStatus(id, status) {
      this.loading = true;
      let param = {
        id: id,
        status: status,
      };
      var d = await this.$axios.put("/admin/status", param);
      if (d.code == 200) {
        this.$message.success(d.message);
      } else {
        this.$message.error(d.message);
      }
      this.loading = false;
    },

    //性别格式化
    sexFormat(row,column,cellValue,index){
      return cellValue=="01"?"男":"女"
    },

    // 分页插件事件
    callFather(parm) {
      this.condition.currentPage = parm.currentPage;
      this.condition.pageSize = parm.pageSize;
      this.init();
    },
    // 搜索事件
    search() {
      this.init();
    },

    //显示编辑界面
    handleEdit: function (name, row) {
      this.editFormVisible = true;
      if (row != undefined && row != "undefined") {
        this.title = "修改";
        this.isAdd = false;
        this.isView = name=="view";
        this.editForm.id = row.id;
        this.editForm.name = row.name;
        this.editForm.sex = row.sex;
        this.editForm.phone = row.phone;
        this.editForm.address = row.address;
        this.editForm.status = row.status;
        this.editForm.remarks = row .remarks;
      } else {
        this.title = "添加";
        this.isAdd = true;
        this.isView = false;
      }
    },

    // 编辑、增加页面保存方法
    submitForm(editData) {
      this.$refs[editData].validate(async (valid) => {
        if (valid) {
          if(this.isAdd){
            //添加            
            var  d = await this.$axios.post("/admin/add",this.editForm)
            if(d.code == 200){
              this.$message.success(d.message)
              this.reload();
            }else{
              this.$message.error(d.message)
            }
            
          }else{
            //编辑
            var  d = await this.$axios.put("/admin/update",this.editForm)
            if(d.code == 200){
              this.$message.success(d.message)
              this.reload();
            }else{
              this.$message.error(d.message)
            }
          }
        } else {
          return false;
        }
      });
    },

    // 删除
      del(id) {
      this.$confirm("确定要删除吗?", "信息", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          this.loading = true
          var d = await this.$axios.delete("/admin/del/"+id)
          if(d.code ==200){
            this.$message.success(d.message)
             this.loading = false
            this.reload();
          }else{
            this.$message.error(d.message)
             this.loading = false
          } 
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    // 选择复选框事件
    selectChange(val) {
      this.selectdata = val;
    },

     // 批量删除
      dels() {
        if(this.selectdata.length<=0){
          return;
        }
      this.$confirm("确定要删除吗?", "信息", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          this.loading = true
          let ids = [];
            for (let i = 0; i < this.selectdata.length; i++) {
              ids.push(this.selectdata[i].id);
            }
          var d = await this.$axios.post("/admin/dels",ids)
          if(d.code ==200){
            this.$message.success(d.message)
             this.loading = false
            this.reload();
          }else{
            this.$message.error(d.message)
             this.loading = false
          } 
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 关闭编辑、增加弹出框
    closeDialog() {
      this.editFormVisible = false;
      this.editForm={
         id: "",
        name: "",
        sex: "",
        phone: "",
        address:"",
        remarks:"",
        status: ""
      }
      this.$refs.editForm.resetFields();
    
    },
  },
};
</script>

<style scoped>
.user-search {
  margin-top: 20px;
}
.userRole {
  width: 100%;
}

</style>

 
 